from django.db import models
from django.contrib.auth.models import User 
from DjangoUeditor.models import UEditorField
from dateutil.relativedelta import relativedelta
from datetime import datetime

from django.contrib.auth import get_user_model
UserP = get_user_model()



# 子网站
class Web(models.Model):
    name = models.CharField('子网站名',max_length=100)
    # categories = models.ManyToManyField('Category')



    class Meta:
        verbose_name = '子网站'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 文章分类
class Category(models.Model):
    web = models.ForeignKey(Web, on_delete=models.DO_NOTHING, verbose_name='子网站', blank=True, null=True)
    name = models.CharField('分类', max_length=100)
    index = models.IntegerField(default=999, verbose_name='分类排序')

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def __setitem__(self, k, v):
        self.k = v

    def __str__(self):
        if self.web:
            show_name = self.name
            return show_name
        else:
            return self.name

# 文章分类
class Category2(models.Model):
    category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name='分类')
    name = models.CharField('子分类', max_length=100)
    index = models.IntegerField(default=999, verbose_name='分类排序')

    class Meta:
        verbose_name = '子分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        show_name = self.name
        return show_name
#文章标签
class Tag(models.Model):
    name = models.CharField('文章标签',max_length=100)
    class Meta:
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
#推荐位
class Tui(models.Model):
    name = models.CharField('推荐位',max_length=100)

    class Meta:
        verbose_name = '推荐位'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# Level
class Level(models.Model):
    name = models.CharField('等级',max_length=100)

    class Meta:
        verbose_name = '等级'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name



#文章
class Article(models.Model):
    title = models.CharField('标题', max_length=70)
    excerpt = models.TextField('摘要', max_length=200, blank=True)
    web = models.ForeignKey(Web, on_delete=models.CASCADE, verbose_name='子网站', blank=True, null=True)
    category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name='分类', blank=True, null=True)
    category2 = models.ForeignKey(Category2, on_delete=models.DO_NOTHING, verbose_name='子分类', blank=True, null=True)
     #使用外键关联分类表与分类是一对多关系
    tags = models.ManyToManyField(Tag,verbose_name='标签', blank=True)
    #使用外键关联标签表与标签是多对多关系
    img = models.ImageField(upload_to='article_img/%Y/%m/%d/', verbose_name='文章图片', blank=True, null=True)
    body = UEditorField('内容', width=800, height=500, 
                    toolbars="full",imagePath='app/images/',filePath='app/files/',
                    upload_settings={"imageMaxSize": 1204000},
                    settings={}, command=None, blank=True
                    )
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户")
    """
     文章作者，这里User是从django.contrib.auth.models导入的。
     这里我们通过 ForeignKey 把文章和 User 关联了起来。
    """
    views = models.PositiveIntegerField('阅读量', default=0)
    tui = models.ForeignKey(Tui, on_delete=models.DO_NOTHING, verbose_name='推荐位', blank=True, null=True)


    
    created_time = models.DateTimeField('发布时间', auto_now_add=True)
    modified_time = models.DateTimeField('修改时间', auto_now=True)

    level = models.ForeignKey(Level, on_delete=models.CASCADE, verbose_name='等级', blank=True, null=True)

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'

    def __str__(self):
        return self.title


# 浏览历史
class History(models.Model):
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户")
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name="文章")
    created_time = models.DateTimeField('浏览时间', default=datetime.now())

    class Meta:
        verbose_name = '浏览历史'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username

# 收藏
class Collection(models.Model):
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户")
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name="文章")

    class Meta:
        verbose_name = '收藏'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username

#Banner
class Banner(models.Model):
    text_info = models.CharField('标题', max_length=50, default='')
    img = models.ImageField('图片', upload_to='banner/')
    link_url = models.URLField('图片链接', max_length=100)
    zone = models.CharField(max_length=50, choices=[('0', '首页'), ('1', '子网站'),], verbose_name="位置", blank=True, null=True)
    web = models.ForeignKey(Web, on_delete=models.CASCADE, verbose_name='子网站', blank=True, null=True)
    category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name='分类', blank=True, null=True)
    category2 = models.ForeignKey(Category2, on_delete=models.DO_NOTHING, verbose_name='子分类', blank=True, null=True)
    is_active = models.BooleanField('是否展示', default=False)

    def __str__(self):
        return self.text_info

    class Meta:
        verbose_name = '广告'
        verbose_name_plural = '广告'


#友情链接
class Link(models.Model):
    name = models.CharField('链接名称', max_length=20)
    linkurl = models.URLField('网址',max_length=100)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '友情链接'
        verbose_name_plural = '友情链接'

# VIP
class Vip(models.Model):
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户")
    web = models.ForeignKey(Web, on_delete=models.CASCADE, verbose_name='子网站', blank=True, null=True)
    # is_all = models.BooleanField('是否全站VIP', default=False)
    kind = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="类型")
    start_time = models.DateTimeField('开始时间', default=datetime.now())
    end_time = models.DateTimeField('结束时间', blank=True, null=True)

    def __str__(self):
        return self.user.username
    class Meta:
        verbose_name = 'VIP'
        verbose_name_plural = 'VIP'

    def save(self, *args, **kwargs):
        if self.kind == '3' and not self.end_time:
            self.end_time = self.start_time + relativedelta(months=3)
        elif self.kind == '6' and not self.end_time:
            self.end_time = self.start_time + relativedelta(months=6)
        elif self.kind == '12' and not self.end_time:
            self.end_time = self.start_time + relativedelta(years=1)
        elif self.kind == '24' and not self.end_time:
            self.end_time = self.start_time + relativedelta(years=2)

        # if not self.web:
        #     self.is_all = True
        return super(Vip, self).save(*args, **kwargs)

# 订单
class Order(models.Model):
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户")
    email = models.EmailField(verbose_name="邮箱")
    truename = models.CharField(max_length=15, verbose_name="真实姓名")
    phone = models.CharField(max_length=11, verbose_name="手机号码")
    company_name = models.CharField(max_length=100, null=True, blank=True, verbose_name="公司名称")
    company_address = models.CharField(max_length=100, null=True, blank=True, verbose_name="公司地址")

    web0 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="全站VIP", blank=True, null=True)
    web1 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="新闻政策", blank=True, null=True)
    web2 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="有机原料", blank=True, null=True)
    web3 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="无机原料", blank=True, null=True)
    web4 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="氟化工", blank=True, null=True)
    web5 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="溴化工", blank=True, null=True)
    web6 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="磷化工", blank=True, null=True)
    web7 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="肥料", blank=True, null=True)
    web8 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="农药", blank=True, null=True)
    web9 = models.CharField(max_length=50, choices=[('3', '3个月'), ('6', '半年'), ('12', '一年'), ('24', '两年')], verbose_name="涂料", blank=True, null=True)

    pay_type = models.CharField(max_length=5, choices=[('0', '支付宝'), ('1', '微信支付')], verbose_name="支付方式")

    price = models.IntegerField(verbose_name="金额")
    complete = models.BooleanField(default=False, verbose_name="完成")
    created_time = models.DateTimeField('发布时间', auto_now_add=True)
    def __str__(self):
        return self.user.username
    class Meta:
        verbose_name = '订单'
        verbose_name_plural = '订单'

# 帮助咨询
class Contact(models.Model):
    user = models.ForeignKey(UserP, on_delete=models.CASCADE, verbose_name="用户", null=True, blank=True)
    email = models.EmailField(verbose_name="邮箱")
    truename = models.CharField(max_length=15, verbose_name="真实姓名")
    phone = models.CharField(max_length=11,verbose_name="手机号码")
    company_name = models.CharField(max_length=100, null=True, blank=True, verbose_name="公司名称")
    company_address = models.CharField(max_length=100, null=True, blank=True, verbose_name="公司地址")
    context = models.TextField(verbose_name="咨询内容")
    created_time = models.DateTimeField('发布时间', auto_now_add=True)

    def __str__(self):
        return self.user.username
    class Meta:
        verbose_name = '帮助咨询'
        verbose_name_plural = verbose_name

class Ad(models.Model):
    name = models.CharField('广告位名称', max_length=50)
    url = models.URLField('广告网址',max_length=200)
    img = models.ImageField('图片', upload_to='ad/')
    is_active = models.BooleanField('是否展示', default=False)

    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '广告位'
        verbose_name_plural = verbose_name

class Page(models.Model):
    name = models.CharField('页面名称', max_length=50)
    body = UEditorField('内容', width=800, height=500,
                        toolbars="full", imagePath='page/images/', filePath='page/files/',
                        upload_settings={"imageMaxSize": 1204000},
                        settings={}, command=None, blank=True
                        )
    index = models.IntegerField('排序', default=0)
    link = models.CharField('链接', max_length=50)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = '页面'
        verbose_name_plural = verbose_name